package hikari.persistence.handlersimpl;

import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.panels.particle.BoxEmitterEditorPanel;
import hikari.gui.panels.particle.CylinderEmitterEditorPanel;
import hikari.gui.panels.particle.RingEmitterEditorPanel;
import hikari.gui.panels.particle.SphereEmitterEditorPanel;
import hikari.objects3d.Basic3dObject;
import hikari.objects3d.Particle3dObject;
import hikari.persistence.file.FileSection;
import hikari.persistence.persisthandlers.IPersistantObject3dReadHandler;
import hikari.persistence.persisthandlers.PersistantHandler;
import java.io.IOException;
import org.swig.jirr.IParticleEmitter;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.SColor;
import org.swig.jirr.dimension2df;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
@WiringComponent(singleton = true, lazy = true)
@PersistantHandler(sectionName = "particle")
public class ParticleLoadHandler implements IPersistantObject3dReadHandler {

    private ParticleLoadHandler() {
    }

    @Override
    public Basic3dObject read(ISceneManager sceneManager, Basic3dObject object, FileSection section) throws IOException {
        Particle3dObject partObj = new Particle3dObject(sceneManager);
        IParticleEmitter emitter = null;
        FileSection partSect = section.readSection(section.getChunk("particleSetting"));
        String emitterType = partSect.readString(partSect.getChunk("emitterType"));
        switch (emitterType) {
            case "boxEm": {
                emitter = BoxEmitterEditorPanel.loadData(partObj.getParticleSystem(), partSect);
                break;
            }
            case "cylinderEm": {
                emitter = CylinderEmitterEditorPanel.loadData(partObj.getParticleSystem(), partSect);
                break;
            }
            case "ringEm": {
                emitter = RingEmitterEditorPanel.loadData(partObj.getParticleSystem(), partSect);
                break;
            }
            case "sphereEm": {
                emitter = SphereEmitterEditorPanel.loadData(partObj.getParticleSystem(), partSect);
                break;
            }
            case "pointEm": {
                emitter = partObj.getParticleSystem().createPointEmitter();
                partObj.getParticleSystem().setEmitter(emitter);
                break;
            }
            default:
                throw new RuntimeException("Error: loading unrecognized emitter type " + emitterType);
        }

        vector3df direction = partSect.readVector(partSect.getChunk("direction"));
        emitter.setDirection(direction);
        int maxAngleDegrees = partSect.readInt(partSect.getChunk("maxAngleDegr"));
        emitter.setMaxAngleDegrees(maxAngleDegrees);
        long maxLifeTime = partSect.readLong(partSect.getChunk("maxLifeTime"));
        emitter.setMaxLifeTime(maxLifeTime);
        long maxPartPerSec = partSect.readLong(partSect.getChunk("maxPartPerSec"));
        emitter.setMaxParticlesPerSecond(maxPartPerSec);
        SColor maxStartColor = partSect.readSColor(partSect.getChunk("maxStartColor"));
        emitter.setMaxStartColor(maxStartColor);
        dimension2df maxStartSize = partSect.readDimension2df(partSect.getChunk("maxStartSize"));
        emitter.setMaxStartSize(maxStartSize);

        long minLifeTime = partSect.readLong(partSect.getChunk("minLifeTime"));
        emitter.setMinLifeTime(minLifeTime);
        long minPartPerSec = partSect.readLong(partSect.getChunk("minPartPerSec"));
        emitter.setMinParticlesPerSecond(minPartPerSec);
        SColor minStartColor = partSect.readSColor(partSect.getChunk("minStartColor"));
        emitter.setMinStartColor(minStartColor);
        dimension2df minStartSize = partSect.readDimension2df(partSect.getChunk("minStartSize"));
        emitter.setMinStartSize(minStartSize);
        partSect.close();
        return partObj;
    }
}